package main

import (
	"fmt"
	"github.com/gofiber/fiber"
	"log"
)

type Person struct {
	Name string `json:"name" xml:"name" form:"name" query:"name"`
	Pass string `json:"pass" xml:"pass" form:"pass" query:"pass"`
}

type SomeStruct struct {
	Name string
	Age  uint8
}

func main() {
	app := fiber.New()

	//app.Use(func(c *fiber.Ctx) {
	//	fmt.Println("First middleware")
	//	c.Next()
	//})

	app.Use("/api", func(c *fiber.Ctx) {
		fmt.Println("Second middleware")
		c.Next()
	})

	app.Get("/", func(c *fiber.Ctx) {
		c.Send("Hello, World!")
	})

	//app.Get("/:name?", func(c *fiber.Ctx) {
	//	if c.Params("name") != "" {
	//		c.Send("Hello " + c.Params("name"))
	//		// => Hello john
	//	} else {
	//		c.Send("Where is john?")
	//	}
	//})

	app.Post("/", func(c *fiber.Ctx) {
		p := new(Person)

		if err := c.BodyParser(p); err != nil {
			log.Fatal(err)
		}

		log.Println(p.Name) // john
		log.Println(p.Pass) // doe

		c.Send("OK")
	})

	app.Post("/myForm", func(c *fiber.Ctx) {
		// Get first value from form field "name":
		fmt.Println(c.FormValue("name"))
		fmt.Println(c.FormValue("sex"))
		fmt.Println(c.FormValue("birth"))
		// => "john" or "" if not exist
	})

	app.Get("/myQuery", func(c *fiber.Ctx) {
		fmt.Println(c.Query("order")) // "desc"
		fmt.Println(c.Query("brand")) // "nike"
	})

	app.Post("/myJson", func(c *fiber.Ctx) {
		fmt.Println("function middleware")
		c.Next()
		},
		func(c *fiber.Ctx) {
			// Create data struct:
			data := SomeStruct{
				Name: "Grame",
				Age:  20,
			}

			if err := c.JSON(data); err != nil {
				c.Status(500).Send(err)
				return
			}
			// => Content-Type: application/json
			// => "{"Name": "Grame", "Age": 20}"

			//if err := c.JSON(fiber.Map{
			//	"name": "Grame",
			//	"age": 20,
			//}); err != nil {
			//	c.Status(500).Send(err)
			//	return
			//}
			// => Content-Type: application/json
			// => "{"name": "Grame", "age": 20}"
	})

	app.Post("/myHeader", func(c *fiber.Ctx) {
		token := c.Get("token")
		fmt.Println(token)
		c.Send(token)
	})

	app.Listen(3000)
}